package com.avalon.core.select;

import com.avalon.core.select.builder.SelectBuilder;
import com.avalon.core.service.AbstractService;

import java.util.ArrayList;

public class OrderFieldList extends ArrayList<OrderField> {
    public OrderField addOrder(String field, String order, AbstractService service) {
        OrderField orderField = new OrderField(field, order, service);
        add(orderField);
        return orderField;
    }

    public OrderField addOrder(OrderField orderField) {
        add(orderField);
        return orderField;
    }

    public StringBuilder getOrderSql(SelectBuilder builder) {
        StringBuilder sql = new StringBuilder();
        for (OrderField orderField : this) {
            StringBuilder orderSql = orderField.getOrderSql(builder);
            if (!orderSql.isEmpty()) {
                if (sql.length() > 0) {
                    sql.append(",");
                }
                sql.append(orderSql);
            }
        }
        return sql;
    }
}
